package com.scisoft.scan.control.depot;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ClearEditText;
import com.scisoft.scan.control.CommonAdapter;
import com.scisoft.scan.control.CommonViewHolder;
import com.scisoft.scan.data.depot.DepotOrderDetailBean;
import com.scisoft.scan.data.depot.DepotOrderDetailScanBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_Depot;

import java.util.ArrayList;
import java.util.List;

public class DepotDetailScanInfoActivity extends Activity {
    private ImageView iv_Back; //返回按钮
    private TextView tv_title;
    private TextView tv_count;
    private TextView tv_scancount;
    private TextView tv_submit;
    private ClearEditText et_serialNo;
    private String orderId;
    private String orderType;
    private String orderState;//0 未完成 1已完成
    DepotOrderDetailBean depotOrderDetailBean;
    //列表
    DepotDetailScanInfoAdapter depotDetailScanInfoAdapter;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLayoutManager;
    float lastY = 0;

    @Override
    @SuppressLint("ClickableViewAccessibility")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//即隐藏标题栏
        setContentView(R.layout.activity_depot_detail_scan_info);

        //参数传递
        Intent intent = getIntent();
        orderId = intent.getStringExtra("orderId");
        orderType = intent.getStringExtra("orderType");
        orderState = intent.getStringExtra("orderState");
        depotOrderDetailBean = (DepotOrderDetailBean) intent.getSerializableExtra("data");
        if (depotOrderDetailBean.serialNoList == null) {
            depotOrderDetailBean.serialNoList = new ArrayList<>();
        }

        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        et_serialNo = findViewById(R.id.et_serialNo);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText(depotOrderDetailBean.partName);
        tv_count = findViewById(R.id.tv_count);
        tv_scancount = findViewById(R.id.tv_scancount);
        tv_submit = findViewById(R.id.tv_submit);
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DEPOT_08(et_serialNo.getUpperText().toString(), depotOrderDetailBean.dtlId);
            }
        });
        //判断完成状态
        RelativeLayout rl_1 = findViewById(R.id.rl_1);
        if (orderState.equals("1")) {
            rl_1.setVisibility(View.GONE);
            tv_submit.setVisibility(View.GONE);
        }
        mRecyclerView = findViewById(R.id.lv_data);
        depotDetailScanInfoAdapter = new DepotDetailScanInfoAdapter(DepotDetailScanInfoActivity.this);
        depotDetailScanInfoAdapter.setMultiPage(false);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int fullWidth = displayMetrics.widthPixels;
        depotDetailScanInfoAdapter.setItemLayout(R.layout.item_scaninfo, R.id.hsv, R.id.layout_show, R.id.layout_hide, fullWidth);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //滚动的时候清空左滑按钮
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        if (Math.abs(event.getY() - lastY) > 5)
                            depotDetailScanInfoAdapter.clearLeftMenuList();
                }
                return false;
            }
        });
        mRecyclerView.setAdapter(depotDetailScanInfoAdapter);

        BindData(this);
    }

    //绑定数据
    private void BindData(Context context) {
        et_serialNo.setText("");
        tv_count.setText(String.valueOf(depotOrderDetailBean.partCount));
        tv_scancount.setText(String.valueOf(depotOrderDetailBean.serialNoList.size()));

        if (depotOrderDetailBean != null && depotOrderDetailBean.serialNoList != null && depotOrderDetailBean.serialNoList.size() > 0) {
            mRecyclerView.setVisibility(View.VISIBLE);
            //数据
            depotDetailScanInfoAdapter.clearLeftMenuList();
            depotDetailScanInfoAdapter.clearAddItem(depotOrderDetailBean.serialNoList);
        } else {
            mRecyclerView.setVisibility(View.INVISIBLE);
        }
    }

    //上传唯一码
    private void DEPOT_08(String serialNo, String dtlId) {
        CommonUtil.showPress(this);
        ScanUpdateTask scanUpdateTask = new ScanUpdateTask(DepotDetailScanInfoActivity.this, dtlId, serialNo);
        scanUpdateTask.execute();
    }

    //删除唯一码
    private void DEPOT_11(String serialNo, String supplierCode, String dtlId) {
        CommonUtil.showPress(this);
        ScanDeleteTask scanDeleteTask = new ScanDeleteTask(DepotDetailScanInfoActivity.this, dtlId, serialNo, supplierCode);
        scanDeleteTask.execute();
    }

    //上传唯一码服务
    class ScanUpdateTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String dtlId;
        private String serialNo;

        ScanUpdateTask(Context context, String dtlId, String serialNo) {
            super();
            mcontext = context;
            this.dtlId = dtlId;
            this.serialNo = serialNo;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Depot.DEPOT_08(orderId, orderType, dtlId, serialNo);
                if (msg.contains("成功")) {
                    List<DepotOrderDetailBean> depotOrderDetailBeanList = WebServiceHelper_Depot.DEPOT_03(orderId, orderType);
                    for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
                        if (depotOrderDetailBeanList.get(i).dtlId.equals(depotOrderDetailBean.dtlId)) {
                            depotOrderDetailBean.serialNoList = depotOrderDetailBeanList.get(i).serialNoList;
                            break;
                        }
                    }
                }

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //删除唯一码服务
    class ScanDeleteTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String dtlId;
        private String serialNo;
        private String supplierCode;

        ScanDeleteTask(Context context, String dtlId, String serialNo, String supplierCode) {
            super();
            mcontext = context;
            this.dtlId = dtlId;
            this.serialNo = serialNo;
            this.supplierCode = supplierCode;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Depot.DEPOT_11(orderId, orderType, dtlId, serialNo, supplierCode);
                if (msg.contains("成功")) {
                    List<DepotOrderDetailBean> depotOrderDetailBeanList = WebServiceHelper_Depot.DEPOT_03(orderId, orderType);
                    for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
                        if (depotOrderDetailBeanList.get(i).dtlId.equals(depotOrderDetailBean.dtlId)) {
                            depotOrderDetailBean.serialNoList = depotOrderDetailBeanList.get(i).serialNoList;
                            break;
                        }
                    }
                }

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //适配器相关功能
    class DepotDetailScanInfoAdapter extends CommonAdapter<DepotOrderDetailScanBean> {

        //构造函数
        public DepotDetailScanInfoAdapter(Context context) {
            super(context);
        }

        @Override
        public void convert(CommonViewHolder holder, final DepotOrderDetailScanBean depotOrderDetailScanBean, int position) {
            //获取相应索引的ItemBean对象
            final DepotOrderDetailScanBean bean = mList.get(position);
            //配件编号
            TextView tv_partNo = holder.getView(R.id.tv_partNo);
            //配件信息
            TextView tv_scanInfo = holder.getView(R.id.tv_scanInfo);

            //设置控件的对应属性值
            tv_partNo.setText(depotOrderDetailBean.partNo);
            tv_scanInfo.setText(bean.supplierCode + "-" + bean.serialNo);

            LinearLayout layout_hide = holder.getView(R.id.layout_hide);
            if (orderState.equals("1")) {
                layout_hide.setVisibility(View.GONE);
            }
            else{
                layout_hide.setVisibility(View.VISIBLE);
                //删除按钮
                TextView tv_delete = holder.getView(R.id.tv_delete);
                tv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //删除
                        DEPOT_11(bean.serialNo,bean.supplierCode,depotOrderDetailBean.dtlId);

                    }
                });
            }

        }
    }
}
